$(function(){
	refresh();
});

function querySecondLevel(){
	clearThirdLevel();
	var firCode = document.getElementById("FirstSelect2").value;
	var data=new jStringBuffer();
	if(firCode != "-1"){
		$("#SecondSelect").html('');
		data.append("<option value='-1'>--请选择--</option>");
		$.getJSON("secondLevel",{firCode:firCode},function(secondLevel,status){
			for(var i=0;i<secondLevel.length;i++){
				data.append("<option value='").append(secondLevel[i].secCode)
				.append("'>").append(secondLevel[i].secName).append("</option>");
			}
			$("#SecondSelect").append(data.toString());
		});
	}
}
function clear2(){
	$('#SecondLevel2').val('');
	$('#ThirdLv2').val('');
	$('#ThirdLevels2').html('');
}

function clearThirdLevel(){
	$("#threeThirdLv").val('');
	$("#threeThirdLv_1").val('');
	$("#ThirdLevels3").html('');
	
}

/*判断数据库中是否已经存在该一级类别*/
function isFirstExist(obj){
	$(obj.parentNode).next().next().html('');
	var firName = $(obj).val().replace(/\s/g, "");
	$.post("isExistFirst",{firName:firName},function(mark){
		if(mark=="1"){
			$(obj.parentNode).next().next().append("<span style='color:red'>数据库已存在该分类！</span>");
		}
	});
}
/**判断输入项中是否有重复的*/
function isThirdExist1(obj){
	$(obj.parentNode).next().next().html('');
	var thiName = $(obj).val().replace(/\s/g, "");
	var hasList = new Array();
	var inputs = document.getElementsByName('thirdLevels1');
	var temp = $('#ThirdLv1').val().replace(/\s/g, "");
	if(temp != "")hasList.push(temp);
	for(var i=0;i<inputs.length;i++){
		if($(inputs[i]).val().replace(/\s/g, "") != ""){
			hasList.push($(inputs[i]).val().replace(/\s/g, ""));
		}
	}
	var count = 0;
	for(var i = 0;i < hasList.length; i++ ){
		if(thiName == hasList[i])count++;
	}
	if(count > 1){
		$(obj.parentNode).next().next().append("<span style='color:red'>请不要输入重复类别名！</span>");
	}
}

/*判断数据库中是否已经存在该二级类别*/
function isTwoExist(obj){
	$(obj.parentNode).next().next().html('');
	var firName = $("#FirstSelect1").find("option:selected").text().replace(/\s/g, "");
	var secName = $(obj).val().replace(/\s/g, "");
	$.post("isExistSecond",{firName:firName,secName:secName},function(mark){
		if(mark=="1"){
			$(obj.parentNode).next().next().append("<span style='color:red'>数据库已存在该分类！</span>");
		}
	});
}


function isThirdExist2(obj){
	$(obj.parentNode).next().next().html('');
	var thiName = $(obj).val().replace(/\s/g, "");
	var hasList = new Array();
	var inputs = document.getElementsByName('thirdLevels2');
	var temp = $('#ThirdLv2').val().replace(/\s/g, "");
	if(temp != "")hasList.push(temp);
	for(var i=0;i<inputs.length;i++){
		if($(inputs[i]).val().replace(/\s/g, "") != ""){
			hasList.push($(inputs[i]).val().replace(/\s/g, ""));
		}
	}
	var count = 0;
	for(var i = 0;i < hasList.length; i++ ){
		if(thiName == hasList[i])count++;
	}
	if(count > 1){
		$(obj.parentNode).next().next().append("<span style='color:red'>请不要输入重复类别名！</span>");
	}
}





/*判断数据库中是否已经存在该三级类别*/
function isThirdExist3(obj){
	$(obj.parentNode).next().next().html('');
	var firName = $("#FirstSelect2").find("option:selected").text().replace(/\s/g, "");
	var secName = $("#SecondSelect").find("option:selected").text().replace(/\s/g, "");
	var thiName = $(obj).val().replace(/\s/g, "");
	$.post("isExistThird",{firName:firName,secName:secName,thiName:thiName},function(mark){
		if(mark=="1"){
			$(obj.parentNode).next().next().append("<span style='color:red'>数据库已存在该分类！</span>");
		}
		else{
			var hasSet = new Array();
			var inputs = document.getElementsByName('thirdLevels3');
			var temp = $('#threeThirdLv').val().replace(/\s/g, "");
			if(temp != "")hasSet.push(temp);
			for(var i=0;i<inputs.length;i++){
				if($(inputs[i]).val().replace(/\s/g, "") != "")hasSet.push($(inputs[i]).val().replace(/\s/g, ""));
			}
			var count = 0;
			for(var i = 0;i < hasSet.length; i++ ){
				if(thiName == hasSet[i])count++;
			}
			if(count > 1){
				$(obj.parentNode).next().next().append("<span style='color:red'>请不要输入重复类别名！</span>");
			}
		}
	});
}

/*添加三级分类*/
function addThirdLevel1(){
	var data=new jStringBuffer();
	data.append("<tr><td></td><td><input name='thirdLevels1' class='form-control' type='text' placeholder='Enter ...' onchange='isThirdExist1(this)'/></td><td></td><td></td></tr> ");
	$("#ThirdLevels1").append(data.toString());
}
function addThirdLevel2(){
	var data=new jStringBuffer();
	data.append("<tr><td></td><td><input name='thirdLevels2' class='form-control' type='text' placeholder='Enter ...' onchange='isThirdExist2(this)'/></td><td></td><td></td></tr> ");
	$("#ThirdLevels2").append(data.toString());
}
function addThirdLevel3(){
	var data=new jStringBuffer();
	data.append("<tr><td></td><td><input name='thirdLevels3' class='form-control' type='text' placeholder='Enter ...' onchange='isThirdExist3(this)'/></td><td></td><td></td></tr> ");
	$("#ThirdLevels3").append(data.toString());
}
$('#submit1').click(function(){
	var firName = $('#FirstLevel').val().replace(/\s/g, "");
	if(firName == "")alert("请添加一级分类");
	else{
		$.post("isExistFirst",{firName:firName},function(mark){
			if(mark=="1")alert("请修改一级分类名称！");
			else{
				var secName = $('#SecondLevel1').val().replace(/\s/g, "");
				if(secName == "")alert("请添加二级分类");
				else{
					var thiLevels =new Array();
					var inputs = document.getElementsByName('thirdLevels1');
					var temp = $('#ThirdLv1').val().replace(/\s/g, "");
					if(temp != "")thiLevels.push(temp);
					for(var i=0;i<inputs.length;i++){
						if($(inputs[i]).val().replace(/\s/g, "") != "")thiLevels.push($(inputs[i]).val().replace(/\s/g, ""));
					}
					if(thiLevels.length==0)alert("您尚未添加三级分类！");
					else{
						var Levels = unique(thiLevels);//去除重复项
						if(Levels.length == thiLevels.length){
							var jsonarray = new Array();
							for(var i=0;i<Levels.length;i++){
								jsonarray[i]={"value":Levels[i]};
							}
							var s = JSON.stringify(jsonarray);
							$.post("addLeveLs_1",{firName:firName,secName:secName,s:s},function(mark){
								if(mark == "1"){
									alert("添加成功！");
									document.getElementById('formone').reset();
									refresh();
								}
								else alert("添加失败！");
							})
						}
						else alert("存在重复项！");
					}
				}
			}
		});
	}
});

$('#submit2').click(function(){
	var firCode = $("#FirstSelect1").val();
	if(firCode == "-1")alert("请选择一级分类！");
	else{
		var firName = $("#FirstSelect2").find("option:selected").text().replace(/\s/g, "");
		var secName = $('#SecondLevel2').val().replace(/\s/g, "");
		if(secName == "-1")alert("请添加二级分类");
		else{
			var thiLevels =new Array();
			var inputs = document.getElementsByName('thirdLevels2');
			var temp = $('#ThirdLv2').val().replace(/\s/g, "");
			if(temp != "")thiLevels.push(temp);
			for(var i=0;i<inputs.length;i++){
				if($(inputs[i]).val().replace(/\s/g, "") != "")thiLevels.push($(inputs[i]).val().replace(/\s/g, ""));
			}
			if(thiLevels.length==0)alert("您尚未添加三级分类！");
			else{
				var Levels = unique(thiLevels);//去除重复项
				if(Levels.length == thiLevels.length){
					var jsonarray = new Array();
					for(var i=0;i<Levels.length;i++){
						jsonarray[i]={"value":Levels[i]};
					}
					var s = JSON.stringify(jsonarray);
					$.post("addLeveLs_2",{firCode:firCode,firName:firName,secName:secName,s:s},function(mark){
						if(mark == "1"){
							alert("添加成功！");
							clear2();
							
						}
						else alert("添加失败！");
					});
				}
				else alert("存在重复项！");
			}
		}
	}
});




$('#submit3').click(function(){
	var firCode = $("#FirstSelect2").val();
	var secCode = $("#SecondSelect").val();
	if(firCode == "-1" || secCode == "-1")alert("请选择上级分类！");
	else{
		var firName = $("#FirstSelect2").find("option:selected").text().replace(/\s/g, "");
		var secName = $("#SecondSelect").find("option:selected").text().replace(/\s/g, "");
		var thiLevels =new Array();
		var inputs = document.getElementsByName('thirdLevels3');
		var temp = $('#threeThirdLv').val().replace(/\s/g, "");
		if(temp != "")thiLevels.push(temp);
		for(var i=0;i<inputs.length;i++){
			if($(inputs[i]).val().replace(/\s/g, "") != "")thiLevels.push($(inputs[i]).val().replace(/\s/g, ""));
		}
		if(thiLevels.length==0)alert("您尚未添加三级分类！");
		else{
			var Levels = unique(thiLevels);//去除重复项
			if(Levels.length == thiLevels.length){//如果没有重复项
				var jsonarray = new Array();
				for(var i=0;i<Levels.length;i++){
					jsonarray[i]={"value":Levels[i]};
				}
				var s = JSON.stringify(jsonarray);
				$.post("addLeveLs_3",{
						firCode:firCode,
						firName:firName,
						secCode:secCode,
						secName:secName,
						s:s
					},function(mark){
					if(mark=="1"){
						alert("添加成功！")
						clearThirdLevel();
					}
					else{
						alert("添加失败！");
					}
				});
			}
		}
	}
});

/**
 * 
 */

function refresh(){
	$('#FirstSelect1').html('');
	$('#FirstSelect2').html('');
	var data=new jStringBuffer();
	$.getJSON("firstLevel",{},function(firstLevel,status){
		data.append("<option value='-1'>--请选择--</option>");
		for(var i=0;i<firstLevel.length;i++){
			data.append("<option value='").append(firstLevel[i].firCode)
			.append("'>").append(firstLevel[i].firName).append("</option>");
		}
		$("#FirstSelect1").append(data.toString());
		$("#FirstSelect2").append(data.toString());
	});
}
/**
 * 去除数组中的重复项
 */
function unique(arr){
	var tmp = new Array();
	for(var m in arr){
		tmp[arr[m]]=1;
	}
	//再把键和值的位置再次调换
	var tmparr = new Array();
	for(var n in tmp){
		tmparr.push(n);
	}
	return tmparr;
}

